from django.core.urlresolvers import reverse
from django.http import *
from django.shortcuts import *
from django import forms
from django.template import RequestContext 
from django.utils.translation import gettext
from ytbase.helper import *
from ytbase.forms.model import *
from ytcommunity.forum.model.board import *
from ytcommunity.forum.helper.context import *
from ytcommunity.forum.setting import BOARD_NUM_PER_PAGE, POST_NUM_PER_PAGE
import ytbase.view.post

class BoardForm(forms.Form):
    ref = ModelChoiceField(YTBoard, required=False, label=gettext(u'Parent Board'))
    key = forms.RegexField(regex=r'\w+', label=gettext(u'Board Key'), max_length=20, help_text=gettext(u'This field is required.'))
    title = forms.CharField(max_length=100, help_text=gettext(u'This field is required.'))
    sort = forms.IntegerField()
    dummy = forms.BooleanField(required=False)
    expand = forms.BooleanField(required=False)

@authorize_admin
def add(request, template):
    if request.method == 'POST':
        form = BoardForm(request.POST)
        if form.is_valid():
            board = YTBoard.get_or_insert_by_name(form.cleaned_data['key'])
            board.ref = form.cleaned_data['ref']
            board.title = form.cleaned_data['title']
            board.sort = form.cleaned_data['sort']
            board.dummy = form.cleaned_data['dummy']
            board.expand = form.cleaned_data['expand']
            board.put()
            return HttpResponseRedirect(reverse('ytcommunity.forum.view.board.list'))
    else:
        form = BoardForm() # An unbound form

    context = {'form': form}
    rc = RequestContext(request, processors=PROCESSORS)
    return render_to_response(template, context, rc)

@authorize_admin
@check_name(YTBoard)
def edit(request, name, template):
    if request.method == 'POST':
        form = BoardForm(request.POST)
        board = None
        if form.is_valid():
            board = YTBoard.get_by_name(form.cleaned_data['key'])
            board.ref = form.cleaned_data['ref']
            board.title = form.cleaned_data['title']
            board.sort = form.cleaned_data['sort']
            board.dummy = form.cleaned_data['dummy']
            board.expand = form.cleaned_data['expand']
            board.put()
            return HttpResponseRedirect(reverse('ytcommunity.forum.view.board.list'))
    else:
        board = YTBoard.get_by_name(name)
        data = {
            'ref':YTBoard.ref.get_value_for_datastore(board),
            'key':board.get_board_key(),
            'title':board.title,
            'sort':board.sort,
            'dummy':board.dummy,
            'expand':board.expand,
        }
        form = BoardForm(data) # An unbound form

    context = {'form': form, 'board': board}
    rc = RequestContext(request, processors=PROCESSORS)
    return render_to_response(template, context, rc)

@check_name(YTBoard)
def display(request, name, template):
    board = YTBoard.get_by_name(name)    
    
    filters = []
    filters.append(lambda q: q.filter('ref =', board.key()))
    filters.append(lambda q: q.order('-update'))
    filters.append(lambda q: q.filter('hot =', False))
    posts, page_posts = Paginator(request, YTPost, filters, POST_NUM_PER_PAGE).get_page_objects()
    
    filters = []
    filters.append(lambda q: q.filter('ref =', board.key()))
    filters.append(lambda q: q.order('-update'))
    filters.append(lambda q: q.filter('hot =', True))
    posts_stick = build_query(YTPost, filters).fetch(1000)
        
    context = {
        'board':board,
        'sub_boards':board.get_sub_boards(),
        'posts':posts,
        'posts_stick':posts_stick,
        'page_info':page_posts,
    }
    rc = RequestContext(request, processors=PROCESSORS)    
    return render_to_response(template, context, rc)     
    
def list(request, template):
    filters = []
    filters.append(lambda q: q.filter('ref =', None))
    filters.append(lambda q: q.order('sort'))
    boards, page = Paginator(request, YTBoard, filters, BOARD_NUM_PER_PAGE).get_page_objects()
    
    context = {
        'expanded_boards': [board for board in boards if board.expand],
        'boards': [board for board in boards if not board.expand],
        'page_info': page,
    }
    rc = RequestContext(request, processors=PROCESSORS)
    return render_to_response(template, context, rc)
